<template>
  <div class="register">
    <div class="nav1" style="">欢迎注册</div>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="用户姓名" prop="nickName" style="color:white" class="change-label-calss">
            <el-input v-model="form.nickName" placeholder="请输入真实姓名" maxlength="30"  :disabled="this.action=='auth'" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="归属公司" prop="etpId" class="change-label-calss">
            <el-select v-model="form.etpId" filterable :disabled="this.action=='auth'"  reserve-keyword placeholder="请选择所属公司"  :loading="loading" style="width: 100%;">
              <el-option v-for="item in etpListByOA" :key="item.id" :label="item.entName" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <!--          <el-form-item label="关联企业id" prop="etpId" v-if="false">-->
          <!--            <el-input v-model="form.etpId" placeholder="" />>-->
          <!--          </el-form-item>-->
        </el-col>
        <el-col :span="8">
          <!--          <el-form-item label="归属部门" prop="deptId">-->
          <!--            <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true" placeholder="请选择归属部门" :disabled="this.action=='auth'" />-->
          <!--          </el-form-item>-->
          <el-form-item label="企业类型" prop="deptId" class="change-label-calss">
            <el-select v-model="form.deptId"  placeholder="请选择企业类型"  style="width: 100%;">
              <el-option v-for="item in bgEnt" :key="item.value" :value="item.value" :label="item.label"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="用户性别"  prop="sex" class="change-label-calss">
            <el-select v-model="form.sex" placeholder="请选择" :disabled="this.action=='auth'" style="width: 100%;">
              <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="手机号码" prop="phonenumber" class="change-label-calss">
            <el-input v-model="form.phonenumber" placeholder="请输入手机号码" maxlength="11"  :disabled="this.action=='auth'" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="邮箱" prop="email" class="change-label-calss">
            <el-input v-model="form.email" placeholder="请输入邮箱" maxlength="50"  :disabled="this.action=='auth'" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item v-if="form.userId == undefined" label="登录账号" prop="userName" class="change-label-calss">
            <el-input v-model="form.userName" placeholder="请输入登录账号" maxlength="30" :disabled="this.action=='auth'" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item v-if="form.userId == undefined" label="用户密码" prop="password" class="change-label-calss">
            <el-input v-model="form.password" placeholder="请输入用户密码" type="password" maxlength="20" show-password :disabled="this.action=='auth'" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="用户类别"  prop="userCategory" class="change-label-calss">
            <el-select v-model="form.userCategory"  placeholder="请选择" :disabled="this.action=='auth'" style="width: 100%;">
              <!--              <el-option v-for="dict in dict.type.sys_user_category" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>-->
              <el-option v-for="item in bgEntType" :key="item.value" :value="item.value" :label="item.label"></el-option>
            </el-select>

          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="身份证号"  prop="idNumber" class="change-label-calss">
            <el-input v-model="form.idNumber" placeholder="请输入" :disabled="this.action=='auth'" maxlength="32" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="是否有边民证"  prop="borderFlag" class="change-label-calss">
            <el-select v-model="form.borderFlag"  placeholder="请选择" :disabled="this.action=='auth'" style="width: 100%;">
              <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="驾照等级" class="change-label-calss">
            <el-select v-model="form.dLevel"  placeholder="请选择" :disabled="this.action=='auth'" style="width: 100%;">
              <el-option v-for="dict in dict.type.sys_drivers_level" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="家庭地址" class="change-label-calss">
            <el-input v-model="form.address" placeholder="请输入" :disabled="this.action=='auth'" maxlength="256"  />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="运输资格" prop="isRoadTransportZz" class="change-label-calss">
            <el-select v-model="form.isRoadTransportZz"  placeholder="请选择" :disabled="this.action=='auth'" style="width: 100%;">
              <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="备注" class="change-label-calss">
            <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :disabled="this.action=='auth'" />
          </el-form-item>
        </el-col>
      </el-row>





    </el-form>
    <div slot="footer" class="dialog-footer" align="center" style="margin-left: 30px">
      <el-button type="primary" @click="submitForm">注 册</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
    <div style="float: right;">
      <router-link class="link-type" :to="'/login'" style="margin-left: 10px;color: #a95812">使用已有账户登录</router-link>
    </div>
  </div>
</template>

<script>
import {getBgEntInfo, getCodeImg, getDepInfo, register, registerAddUser} from "@/api/login";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {validateIdno} from "@/utils/validate";

export default {
  name: "Register",
  dicts: ['sys_normal_disable', 'sys_user_sex','sys_vehicle_authStatus','sys_user_category','sys_yes_no','sys_drivers_level'],
  components: {Treeselect},
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.password !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      form:{},
      bgEnt: [
        { label: "报关企业", value: "208" }
      ],
      bgEntType: [
        { label: "企业用户", value: "2" }
      ],
      codeUrl: "",
      // 部门树选项
      deptOptions: [],
      // 岗位选项
      postOptions: [],
      // 角色选项
      roleOptions: [],
      action:'noAuth',
      avatarImage: [],
      etpListByOA:[],
      registerForm: {
        username: "",
        password: "",
        confirmPassword: "",
        code: "",
        uuid: ""
      },
      registerRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" },
          { min: 2, max: 20, message: '用户账号长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" },
          { min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, trigger: "blur", message: "请再次输入您的密码" },
          { required: true, validator: equalToPassword, trigger: "blur" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      // 表单校验
      rules: {
        userName: [
          {required: true, message: "用户名称不能为空", trigger: "blur"},
          {min: 2, max: 20, message: '用户名称长度必须介于 2 和 20 之间', trigger: 'blur'}
        ],
        etpId: [
          {required: true, message: "请选择所属公司", trigger: "blur"}
        ],
        entName: [
          {required: true, message: "归属公司不能为空", trigger: "blur"},
        ],
        sex: [
          {required: true, message: "用户性别不能为空", trigger: "blur"},
        ],
        deptId: [
          {required: true, message: "归属部门不能为空", trigger: "blur"},
        ],
        idNumber: [
          {required: true,validator:validateIdno, trigger: "blur"},
        ],
        nickName: [
          {required: true, message: "用户姓名不能为空", trigger: "blur"}
        ],
        password: [
          {required: true, message: "用户密码不能为空", trigger: "blur"},
          {min: 5, max: 20, message: '用户密码长度必须介于 5 和 20 之间', trigger: 'blur'}
        ],
        email: [
          {
            type: "email",
            message: "'请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ],
        // entName: [
        //   {
        //     required: true, message: "所属公司不能为空", trigger: "blur"
        //   }
        // ],
        userCategory: [
          {required: true, message: "请选择用户类别", trigger: "blur"}
        ],
        isRoadTransportZz: [
          {
            validator: (rule, value, callback) => {
              if (this.form.userCategory == '5' && this.form.isRoadTransportZz == null ) {
                callback(new Error('请选择是否具备道路运输资格'))
              }  else{
                callback()
              }
            },
            trigger: "blur"}
        ],
        phonenumber: [
          {
            required: true,
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        postIds: [
          {required: true, message: "请选择岗位信息", trigger: "blur"}
        ],

        roleIds: [
          {required: true, message: "请选择角色信息", trigger: "blur"}
        ],


      },
      loading: false,
      captchaOnOff: true
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
      this.$refs.tree.filter(val);
    }
  },
  created() {
    // this.getTreeselect();
    // this.getConfigKey("sys.user.initPassword").then(response => {
    //   this.initPassword = response.msg;
    // });
    this.getEtpsList();
  },
  methods: {
    // 表单重置
    reset() {
      this.form = {
        userId: undefined,
        deptId: undefined,
        userName: undefined,
        nickName: undefined,
        password: undefined,
        phonenumber: undefined,
        email: undefined,
        sex: undefined,
        status: "0",
        remark: undefined,
        postIds: [],
        roleIds: [],
        authStatus:'0',
        etpId: null,
        entName: null,
        borderFlag: null,
        dLevel: null,
        imageInfo: null
      };
      this.avatarImage = []
      this.resetForm("form");
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      getDepInfo().then(response => {
        this.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.deptId = data.id;
      this.getList();
    },
    /** 查询企业列表 */
    getEtpsList() {
      getBgEntInfo().then(response => {
        this.etpListByOA = response.data;

      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // if (this.form.userId != undefined) {
          //   updateUser(this.form).then(response => {
          //     if(this.action == 'auth'){
          //       this.$modal.msgSuccess("认证成功");
          //     }
          //     if(this.action == 'update'){
          //       this.$modal.msgSuccess("修改成功");
          //     }
          //
          //     this.open = false;
          //     this.getList();
          //   });
          // } else {
          //
          // }
          // registerAddUser(this.form).then(response => {
          //   this.$modal.msgSuccess("新增成功");
          //   this.open = false;
          //   this.getList();
          // });
          this.loading = true;
          registerAddUser(this.form).then(res => {
            const username = this.form.nickName;
            this.$alert("<font color='red'>恭喜你，您的账号 " + username + " 注册成功！</font>", '系统提示', {
              dangerouslyUseHTMLString: true
            }).then(() => {
              this.$router.push("/login");
            }).catch(() => {});
          }).catch(() => {
            this.loading = false;

          })

        }
      });
    },
    // 取消按钮
    cancel() {
      this.reset();
    },
    getCode() {
      getCodeImg().then(res => {
        this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff;
        if (this.captchaOnOff) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.registerForm.uuid = res.uuid;
        }
      });
    },
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true;
          register(this.registerForm).then(res => {
            const username = this.registerForm.username;
            this.$alert("<font color='red'>恭喜你，您的账号 " + username + " 注册成功！</font>", '系统提示', {
              dangerouslyUseHTMLString: true
            }).then(() => {
              this.$router.push("/login");
            }).catch(() => {});
          }).catch(() => {
            this.loading = false;
            if (this.captchaOnOff) {
              this.getCode();
            }
          })
        }
      });
    },
    // 更新关联企业名称
    unionEntName() {
      for (let i = 0; i < this.etpListByOA.length; i++) {
        if (this.etpListByOA[i].entName == this.form.entName) {
          this.registerForm.etpId = this.etpListByOA[i].id;
          break;
        }
      }
    },
    /** 填充上传的图片地址信息*/
    handleFillImage(data) {
      this.form.imageInfo = data
    },
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.register {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/home_img.png");
  background-size: cover;
  //position: fixed;
  //top:20px;
  //left: 50px;
  width:100%;
}
.el-row{
  display:flex;
  flex-wrap: wrap;
}
.change-label-calss  .el-form-item__label{
  color: white;
}
.nav1{
  position: fixed;
  left: 55%;
  top:8%;
  font-size: 30px;
  color: white;
  font-weight: bold;
  padding: 30px 0 0 60px;
}
</style>
